package com.one.odf;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.concurrent.CountDownLatch;

/**
 * @description:
 * @author: zero
 * @date: 2020/1/21
 */
@Component
@Log4j2
public class DataService {

    private static final String p = "F:\\WORKSPACE\\ONESPORTS\\odf\\src\\main\\resources\\odf\\";


    @Async
    void odfData(String d, String ds, CountDownLatch countDownLatch){
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://results.wuhan2019mwg.cn/Data/OVRData/%s/Unit/%s/%s/data.json";
        String u = "https://results.wuhan2019mwg.cn/Data/Schedule/Discipline/%s/data.json?random=0.034001434266692554";
        String pp = p + ds;
        File file = new File(pp);
        if (!file.exists()){
            file.mkdirs();
        }
        try {
         String uu = String.format(u,ds);
         String rs = restTemplate.getForObject(uu,String.class);
         log.info("=========>{}",rs);
         JSONArray jsonArray = JSONArray.parseArray(rs);
         jsonArray.forEach(t->{
             JSONObject o = (JSONObject) t;
             String UnitRSCCode = o.getString("UnitRSCCode");
             String uurl = String.format(url,d,ds,UnitRSCCode);
             log.info("=====uurl{}",uurl);
             String fileName = pp + "\\" + UnitRSCCode + "_data_"+ d +"_.json";
             File f = new File(fileName);
             if (f.exists()){
                 return;
             }
             String rss = restTemplate.getForObject(uurl,String.class);
             try {
                 f.createNewFile();
                 FileOutputStream fileOutputStream = new FileOutputStream(fileName);
                 fileOutputStream.write(rss.getBytes());
                 fileOutputStream.close();
             } catch (Exception e) {
                 log.error("写入文件异常",e);
             }
         });
          String up = "https://results.wuhan2019mwg.cn/commonCode/phase.json";
          String phase = restTemplate.getForObject(up,String.class);
          JSONObject p1 = JSON.parseObject(phase);

          p1.keySet().forEach(t1->{
              if (t1.indexOf(ds)<0){
                  return;
              }
              JSONObject p2 = p1.getJSONObject(t1);
              p2.keySet().forEach(t2->{
                  JSONObject p3 = p2.getJSONObject(t2);
                  p3.keySet().forEach(t3->{
                      String uurl = String.format(url,d,ds,t3);
                      log.info("=====phase=====\n==={}==\r",uurl);

                      String fileName = pp + "\\" + t3 + "_data_"+ d +"_.json";
                      File f = new File(fileName);
                      if (f.exists()){
                          return;
                      }
                      String rss = restTemplate.getForObject(uurl,String.class);

                      try {
                          f.createNewFile();
                          FileOutputStream fileOutputStream = new FileOutputStream(fileName);
                          fileOutputStream.write(rss.getBytes());
                          fileOutputStream.close();
                      } catch (Exception e) {
                          log.error("写入文件异常",e);
                      }
                  });
              });
          });


        }catch (Exception e){
         log.error(e.getMessage(),e);
     }
        countDownLatch.countDown();
    }
}
